package main

import (
	"github.com/gin-gonic/gin"
	"time"
)

type Person struct {
	Name string `form:"name"`
	Address string `form:"address"`
	Birthday time.Time `form:"birthday" time_format:"2006-01-02"`
}

func IPAuthMiddleware() gin.HandlerFunc {
	return func(c *gin.Context) {
		ipList:= []string{
			"127.0.0.2",
		}
		flage := false
		clentIp:=c.ClientIP()
		for _, host :=range ipList {
			if clentIp ==host {
				flage = true
			}
		}
		if !flage{
			c.String(401, "%s, not in ipList")
		}
	}
}

func main(){
	r := gin.New()
	// gom Logger 为打印log的中间件，打印 链接信息 状态码 时延
	// gin recovery 即使发生 panic 框架也不会挂
	r.Use(IPAuthMiddleware())
	r.GET("/testing", testing)
	r.Run()
}

func testing (c *gin.Context) {
	var person Person
	if err:=c.ShouldBind(&person); err == nil {
		c.String(200, "%v", person)
	} else {
		c.String(200, "person bind error: %v", err)
	}
}